import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

public class FileServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        String path = "";
        String updateType = req.getParameter("updateType");
        switch (Integer.parseInt(updateType)) {
            case 1://整包升级
                path = this.getServletContext().getRealPath("/files/1.apk");
                break;
            case 2:
                path = this.getServletContext().getRealPath("/files/2.wgt");
                break;
            case 3:
                break;
        }

        File f = new File(path);
        if (f.exists()) {

            FileInputStream fis = new FileInputStream(f);
            String filename = URLEncoder.encode(f.getName(), "utf-8"); //解决中文文件名下载后乱码的问题
            byte[] b = new byte[fis.available()];
            fis.read(b);
            resp.setCharacterEncoding("utf-8");
            resp.setHeader("Content-Disposition", "attachment; filename=" + filename + "");
            //获取响应报文输出流对象
            ServletOutputStream out = resp.getOutputStream();
            //输出
            out.write(b);
            out.flush();
            out.close();
        }

    }

}
